/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpaintball;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 *
 * @author ondra
 */
public class Player extends MovableGameObject {

    private int     acceleration;
    private int     speedLimit;

    private InputController input;
    private Image sprite;

    Player(int x, int y, double frametime) {

        super(x, y, 19, 30, 9, 24, 0, frametime);

        // set behaviour
        acceleration = 8;
        speedLimit = 120;

        // Load player image
        sprite = ImageManager.getImage(ImageManager.IMG_PLAYER_BLUE);

    }

    public void drawSprite(Graphics2D g2d) {

        g2d.drawImage(sprite, position.getX(), position.getY(),  null);
    }

    public void setInput(InputController _input) {
        input = _input;
    }

    public void move() {

        if(input == null) return;

        if(input.isLeft() && speedX > -speedLimit) {
            speedX -= acceleration;
        }
        if(input.isRight() && speedX < speedLimit ) {
            speedX += acceleration;
        }
        if(input.isUp() && speedY > -speedLimit) {
            speedY -= acceleration;
        }
        if(input.isDown() && speedY < speedLimit) {
            speedY += acceleration;
        }

        // slowly decrease speed
        if(!input.isLeft() && !input.isRight() && speedX != 0) {
            if(speedX > 0) {
                speedX -= acceleration;
            } else {
                speedX += acceleration;
            }
        }
        if(!input.isUp() && !input.isDown() && speedY != 0) {
            if(speedY > 0) {
                speedY -= acceleration;
            } else {
                speedY += acceleration;
            }
        }
        savePosition();
        position.relativeMove(speedX*frametime, 0);
        if(collidesWith(collisionObject)) restorePosition();

        savePosition();
        position.relativeMove(0, speedY*frametime);
        if(collidesWith(collisionObject)) restorePosition();

        savePosition();
        position.setRotation(input.getMouseX(), input.getMouseY());
        if(collidesWith(collisionObject)) restorePosition();
    }

    public Paintball createPaintball() {
        return new Paintball(position.getPoleX(), position.getPoleY(), position.getAngle(), frametime);
    }

}
